/**
 * jims
 */
package com.jims.master.modules.reg.service;

import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.modules.reg.dao.ClinicScheduleDao;
import com.jims.master.modules.reg.entity.ClinicSchedule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 出诊安排Service
 *
 * @author CHENXY
 * @version 2017-02-27
 */
@Service
@Transactional(readOnly = false)
public class ClinicScheduleService extends CrudService<ClinicScheduleDao, ClinicSchedule> {

    @Autowired
    private ClinicScheduleDao clinicScheduleDao;


    public ClinicSchedule get(String id) {
        return super.get(id);
    }

    public List<ClinicSchedule> findList(ClinicSchedule clinicSchedule) {
        return super.findList(clinicSchedule);
    }

    public Page<ClinicSchedule> findPage(Page<ClinicSchedule> page, ClinicSchedule clinicSchedule) {
        return super.findPage(page, clinicSchedule);
    }

    /**
     * 保存门诊安排
     *
     * @param clinicSchedule
     * @return
     * @Author chenxy
     */
    @Transactional(readOnly = false)
    public int save(ClinicSchedule clinicSchedule) {
        int result = 0;
        String[] outFlagArr = clinicSchedule.getOutFlag().split(",");
        String[] limitPreNumArr = clinicSchedule.getLimitPreNum().split(",");
        String[] limitNumArr = clinicSchedule.getLimitNum().split(",");
        int delrows = this.deleteByClinicIndexId(clinicSchedule);
        List<ClinicSchedule> list = this.findSaveData(clinicSchedule.getClinicIndexId(), outFlagArr, limitPreNumArr, limitNumArr);
        for (ClinicSchedule c : list) {
            result += super.save(c);
        }
        return result + delrows;
    }

    /**
     * chenxy
     *
     * @param clinicIndexId  号别Id
     * @param outFlagArr     出诊标志数组
     * @param limitPreNumArr 限预约号数数组
     * @param limitNumArr    限号数数组
     * @return
     */
    public List<ClinicSchedule> findSaveData(String clinicIndexId, String[] outFlagArr, String[] limitPreNumArr, String[] limitNumArr) {
        List<ClinicSchedule> resultVal = new ArrayList<ClinicSchedule>();
        //28条记录
        for (int i = 1; i < outFlagArr.length + 1; i++) {
            if (outFlagArr[i - 1].equals("1")) {
                ClinicSchedule clinicSchedule = new ClinicSchedule();
                //通过出诊标志的下标获取
                int a = i % 7;
                int b = i / 7;
                clinicSchedule.setDayOfWeek(a + "");

                clinicSchedule.setTimeDesc((b == 0 ? 1:a==0?b:b+1 ) + "");
                clinicSchedule.setClinicIndexId(clinicIndexId);
                clinicSchedule.setLimitNum(limitNumArr[i - 1]);
                clinicSchedule.setLimitPreNum(limitPreNumArr[i - 1]);
                resultVal.add(clinicSchedule);
            }
        }
        return resultVal;
    }


    public int deleteByClinicIndexId(ClinicSchedule clinicSchedule) {
        return this.clinicScheduleDao.deleteByClinicIndexId(clinicSchedule.getClinicIndexId());

    }

    @Transactional(readOnly = false)
    public int delete(ClinicSchedule clinicSchedule) {
        return super.delete(clinicSchedule);
    }

    /**
     * chenxy
     * 根据号别ID查询出诊安排
     *
     * @param clinicIndexId
     * @return
     */
    public List<ClinicSchedule> getByClinicIndexId(String clinicIndexId) {
        return this.clinicScheduleDao.getByClinicIndexId(clinicIndexId);
    }
}